<template>
    <!-- 轮播图容器 -->
    <div class="banner-container">
        <!-- 轮播项 -->
        <div
            class="banner-slide"
            v-for="item in imgList"
            :key="item.id"
            :class="{ active: activeIndex === item.id }"
        >
          <router-link :to="routerURl.anime+item.id">
            <img :src="item.imgUrl" :alt="item.name">
            <div class="banner-text">
              <h2>{{ item.name }}</h2>
              <p>{{ item.update_time }}</p>
            </div>
          </router-link>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import {routerURl} from "@/utils/Request.js";

const props = defineProps({
    imgList: {
        type: Array,
        default: () => [
            {
                id: 1,
                imgUrl: "http://localhost:7150/src/assets/image/lbt1.png",
                name: "鬼灭之刃 第三季",
                update_time: "每周三更新"
            },
            {
                id: 2,
                name: "我推的孩子",
                imgUrl: "http://localhost:7150/src/assets/image/lbt2.png",
                update_time: "每周三更新"
            },
            {
                id: 3,
                name: "地狱乐",
                imgUrl: "http://localhost:7150/src/assets/image/lbt3.png",
                update_time: "每周六更新"
            }
        ]
    },
    initialActive: {
        type: Number,
        default: 1 //默认开始id
    },
    interval: {
        type: Number,
        default: 5000 // 默认5秒切换一次
    }
});

const activeIndex = ref(props.initialActive);
let timer = null;

// 自动轮播函数
const startAutoPlay = () => {
    timer = setInterval(() => {
        activeIndex.value = activeIndex.value >= props.imgList.length ? 1 : activeIndex.value + 1;
    }, props.interval);
};

// 停止自动轮播
const stopAutoPlay = () => {
    if (timer) {
        clearInterval(timer);
        timer = null;
    }
};

// 监听activeIndex变化
watch(activeIndex, (newVal) => {
    // console.log("当前轮播图:", newVal);
});

onMounted(() => {
    startAutoPlay();
});

onBeforeUnmount(() => {
    stopAutoPlay();
});
</script>

<style scoped>
.banner-container {
    flex: 2;
    height: 330px;
    border-radius: 8px;
    overflow: hidden;
    position: relative;
}

.banner-slide {
    position: absolute;
    width: 100%;
    height: 100%;
    opacity: 0;
    transition: opacity 1s ease-in-out;
}

.banner-slide.active {
    opacity: 1;
}

.banner-slide img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.banner-text {
    position: absolute;
    bottom: 20px;
    left: 20px;
    color: white;
    background-color: rgba(0, 0, 0, 0.5);
    padding: 10px 15px;
    border-radius: 5px;
}
</style>
